import store from '@/store';
import axios from 'axios';

const instance = axios.create({
  baseURL: process.env.VUE_APP_API,
  timeout: 10000,
});

instance.interceptors.request.use(
  config => {
    store.commit('setLoading', true);
    store.commit('setError', { status: false });
    return config;
  },
  error => {
    return Promise.reject(error);
  },
);

instance.interceptors.response.use(
  response => {
    store.commit('setLoading', false);
    return response;
  },
  err => {
    const { error } = err.response.data;
    store.commit('setError', { status: true, message: error });
    store.commit('setLoading', false);
    return Promise.reject(error);
  },
);

export default instance;
